Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
#### DDE And Visual Basic Objects The only Visual Basic objects that can serve as the destination in a DDE conversation are the Text Box, Picture Box, and Label controls. Each of these controls has three properties that determine the details of the conversation: • LinkTopic specifies the application and topic of the conversation. • LinkItem specifies the data item. • LinkMode specifies one of the three link modes (automatic, manual, or notify). Any Visual Basic form (including Multiple Document Interface [MDI] forms) can be the source in a DDE conversation. A form’s LinkTopic property specifies the topic name that the destination application will use to refer to the form in a DDE conversation. The item in a conversation can be a Text Box, Picture Box, or Label control on the form. In the DDE conversation, the destination application specifies the control’s Name property as the item. To have a Text Box, Picture Box, or Label act as a DDE destination, use the control’s LinkTopic property to specify both the source application and the source topic. The property must list the application, followed by a vertical pipe character, CHR$(124), and the topic name. To link a Text Box to data in the Excel worksheet REPORT.XLS, we would write:
    Text1.LinkTopic = “EXCEL|REPORT.XLS”
or
    Text1.LinkTopic = “EXCEL” & CHR$(124) & “ REPORT.XLS”
The control’s LinkItem property specifies the data item to be linked. The precise content and format of the LinkItem property varies from one source application to another (refer to the source application’s documentation for details). For spreadsheet programs, such as Excel and 1-2-3, row and column coordinates are usually meaningful. For word processors, such as Microsoft Word, the same is true for bookmark names. The destination control’s LinkMode property determines the way in which the link will be updated. The default setting is 0-None, which specifies no DDE conversation. When you want the control to be an active DDE destination, three settings are possible: 1-Automatic, 2-Manual, and 3-Notify. (I explained the way these three update modes work earlier in this chapter.) One more destination control property is important to the picture. The LinkTimeout property specifies how long a Visual Basic DDE destination control will wait for the source application to respond to a DDE message before generating an error. The wait time is specified in tenths of seconds, and the default setting is 50, or 5 seconds. To obtain the longest possible wait, 65535 tenths of a second (approximately 1 hour and 49 minutes), set LinkTimeout to -1. Using Visual Basic controls as the source in a DDE conversation also involves properties. Because all DDE conversations are initiated by the destination application, in order to act as a DDE source, a Visual Basic application simply responds to the DDE messages that are sent by the destination. To initiate a DDE link, the message sent by the destination must specify three pieces of information: application, topic, and item. How does this information identify a Visual Basic control? Here are the details: • Application—The name of the Visual Basic source application. If the application is running in the Visual Basic development environment, it is the project name without the .VBP extension. If the application is running as a standalone executable, it is the Visual Basic application name without the .EXE extension. In code, we can use the App object’s EXEName property to obtain the application name. • Topic—The LinkTopic property of the Visual Basic form that contains the control with the data to be linked. This property’s default setting is in the format Form1, Form2, and so on. If a form will serve as a DDE topic, change its LinkTopic property to an appropriate name. • Item—The Name property of the control (Text Box, Picture Box, or Label) that contains the data to be linked. The form’s LinkMode property controls whether a destination application is permitted to initiate a DDE conversation with one or more controls on the form. If LinkMode is set to 0-None (the default), DDE conversations are not permitted with any control on the form. If LinkMode is set to 1-Source, any Label, Picture Box, or Text Box on the form can serve as the source in a DDE link with any destination application that requests it. Once a DDE link exists, Visual Basic automatically notifies the destination application whenever a change occurs in the contents of a linked control. If you set LinkMode to 1-Source at design time, you can change it to 0-None and back again in code at runtime. If you set LinkMode to 0-None at design time, you cannot change it to 1-Source at runtime. Let’s look at a concrete example: Suppose we have a Visual Basic project named SALES.VBP, with a resulting EXE file named SALES.EXE that contains a form with its LinkTopic property set to SalesFigures and its LinkMode property set to 1-Source. Suppose also that this form contains a Text Box with its Name property set to Total. A destination application could establish a DDE link with that Text Box by specifying SALES as the application, SalesFigures as the topic, and Total as the item. #### DDE Events As you might expect, a number of events are associated with DDE conversations. The specific events and the way you use them differ slightly, depending on whether the Visual Basic object is the source or the destination in the DDE conversation. The LinkOpen event occurs when a Visual Basic control acting as a destination successfully initiates a DDE conversation. The event procedure looks like this:
    Sub CtlName_LinkOpen ([Index as Integer], Cancel as Integer)
    . . .
    End Sub
The Index argument identifies the control if it is part of a control array. The Cancel argument is not used for LinkOpen events when the control is the destination. You use the destination control’s LinkOpen event procedure to perform those tasks, such as opening files, that are required when a DDE link is established. You can also use this event to assist with program debugging. The LinkOpen event also occurs when an external destination application initiates a DDE conversation with a Visual Basic source form. Under these circumstances, the LinkOpen event procedure has the following structure:
    Sub FormName_LinkOpen (Cancel as Integer)
    . . .
    End Sub
If the Cancel argument is left at its initial value of False, the link is established. To prevent the link from being established, code in the event procedure can set Cancel to any nonzero value, and Visual Basic will not permit the link to be created. You can use the source LinkOpen event procedure to keep track of the number of DDE links sourced by the Visual Basic program, refusing to establish new links once the total number becomes too large for satisfactory performance. A LinkClose event will occur if the DDE conversation is terminated for either a destination control or a source form. The event procedure receives no information about why the conversation was terminated. A LinkError event will occur for either a destination control or a source form under certain error conditions. Visual Basic’s standard error-handling procedures are invoked if a DDE error occurs while Basic code is executing, enabling you to trap and handle the error in the usual fashion (as was explained in Chapter 7). The LinkError event is triggered only if an error occurs when no Basic code is executing. Occurrences that can cause errors include transfer of data in a format that the destination cannot handle and operating-system resource shortages. I will cover more about handling DDE errors later in this chapter. A LinkNotify event will occur for destination controls where the LinkMode property has been set to 3-Notify. This event is triggered when the source data changes. Code in the LinkNotify event procedure can use the LinkRequest method to update the destination immediately with the modified data. When the new data is not needed immediately, a flag can be set indicating that an update must be performed later.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.